package model

import (
	"fmt"
	"hellow/db"
	// "github.com/gin-gonic/gin"
)



type User struct {
	Id int `json:"id"`
	Username string `json:"username" form:"username" binding:"required"`
	Phone string `json:"phone" form:"phone" binding:"required"`
	Password string `json:"password" form:"password" binding:"required"`
	Age int `json:"age" form:"age"`
	Createtime string `json:"createtime" form:"createtime" binding:"required"`
}

// 根据id查询
func GetUserInfoById(id string) (result []User, errorMsg string){
	DB := db.ConnectMysql()
	var userInfo []User
	err := DB.Select(&userInfo, "select * from users where id = ?", id)
	if err != nil {
		fmt.Println("mysql exec error: " + err.Error())
  		errorMsg = "mysql exec error: " + err.Error()
    }
    return userInfo,errorMsg
}

// 新增数据
func InsertUserInfo(username string, age int, createtime string, password string, phone string)(errorMsg string) {
	DB := db.ConnectMysql()
	_, err := DB.Exec("insert into users (username, age, createtime, password, phone ) values (?, ?, ?, ?, ?)", username, age, createtime, password, phone)
	if err != nil {
		errorMsg = "mysql exec error:" + err.Error()
	}
	return errorMsg
}

// 列表
func GetListByCondition(username string, phone string, page int) (result []User, errorMsg string) {
	DB := db.ConnectMysql()
	// var result []User
	err:=DB.Select(&result, "select * from users where username = ? and phone = ? limit ?,10", username, phone, page)
	if err != nil {
		errorMsg = "mysql exec error:" + err.Error()
	}
	return result,errorMsg
}
